﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Zxb.DayWork.Logic;
using System.IO;

namespace Zxb.OneTimeWork
{
    public class DianpuUrl : BatchWorker
    {
        DataTable domainDT;
        DataTable dianpuDT;

        private static DataTable ReadDtCityDomain()
        {
            string sql = "select ZxbCityID,CityName,Domain from domaininfo with(nolock)";
            return DAL.ReadDt_Retry_ReturnNewIfNull(DB.ZxbReader, sql);
        }

        private static DataTable ReadDtDianpu()
        {
            string sql = "select UserID,DealerName,CityID,City from UserDealerExtend with(nolock) where IsDel=0";
            return DAL.ReadDt_Retry_ReturnNewIfNull(DB.TaskReader, sql);
        }

        protected override void ReadData()
        {
            domainDT = ReadDtCityDomain();
            dianpuDT = ReadDtDianpu();
        }

        protected override void ProcessData()
        {
            var query =
                from d in dianpuDT.AsEnumerable()
                join c in domainDT.AsEnumerable()
                on d.Field<int>("CityID") equals c.Field<int>("ZxbCityID") into dc
                from item in dc.DefaultIfEmpty()
                select new
                {

                    CityName = d.Field<string>("City"),
                    DianpuID = d.Field<int>("UserID"),
                    DianpuName = d.Field<string>("DealerName"),
                    CityDomain = (item == null) ? "http://home.soufun.com/" : item.Field<string>("Domain")
                };
            var query2 =
                from q in query
                orderby q.CityName,q.DianpuName
                select new
                {
                    CityName = q.CityName,
                    DianpuName = q.DianpuName,
                    DianpuID = q.DianpuID,
                    DianpuUrl = string.Format("{0}zhuangxiu/designer/designerinfo{1}/", q.CityDomain, q.DianpuID)
                };

            StreamWriter sw = new StreamWriter("D:\\dianpu.txt");

            foreach (var q in query2)
            {
                sw.WriteLine(string.Format("{0},{1},{2},{3}", q.CityName, q.DianpuName, q.DianpuID, q.DianpuUrl));
            }

            sw.Close();

            //var list = query2.ToList();

            //DataTable dt = new DataTable();
            //DataColumn dc1 = new DataColumn("城市", typeof(string));
            //dt.Columns.Add(dc1);
            //DataColumn dc2 = new DataColumn("店铺", typeof(string));
            //dt.Columns.Add(dc2);
            //DataColumn dc3 = new DataColumn("店铺ID", typeof(string));
            //dt.Columns.Add(dc3);
            //DataColumn dc4 = new DataColumn("店铺Url", typeof(string));
            //dt.Columns.Add(dc4);

            //foreach (var q in query2)
            //{
            //    DataRow row = dt.NewRow();
            //    row["城市"] = q.CityName;
            //    row["店铺"] = q.DianpuName;
            //    row["店铺ID"] = q.DianpuID;
            //    row["店铺Url"] = q.DianpuUrl;
            //    dt.Rows.Add(row);
            //}
            //int ccc = dt.Rows.Count;

        }

        protected override void WriteData()
        {
            //DAL.TVPUpdate(domainDT, "EstateManageUpdateCityID", "UdtEstateManageUpdateCityID");
        }
    }
}
